import 'package:get/get.dart';
import 'package:sp_util/sp_util.dart';
import 'package:yp/config/config.dart';
import 'package:yp/services/api.dart';

import 'state.dart';

class FeedbackHistoryLogic extends GetxController {
  final state = FeedbackHistoryState();

  @override
  void onInit() {
    super.onInit();
    queryHistory();
    if (Get.arguments != null) {
      state.refreshCtrl = Get.arguments;
      update();
    }
  }

  void queryHistory() {
    var paramsIntegral = {
      'access_token': SpUtil.getString('token'),
      // 'head_ec_shop_num_id': SpUtil.getInt('storeShopNum'),
      'head_ec_shop_num_id': SpUtil.getObject(Config.GET_SHOP_INFO)['shop_id'],
      'group_query_sign': 1, //分组信息查询标识,1：查询分组信息 2：不查询
      'usr_num_id': SpUtil.getInt('empeNumId'),
      'page_num': state.pageIndex,
      'page_size': 10
    };
    Api.queryFeedbackLabels(paramsIntegral).listen((event) {
      if (event['code'] == 0) {
        var _list = event['feedback_content_list'];
        if (state.pageIndex == 1) {
          state.dataList = _list;
        } else {
          state.dataList.addAll(_list ?? []);
        }
      }
      if (state.refreshCtrl.isRefresh) {
        state.refreshCtrl.refreshCompleted();
      }
      if (state.refreshCtrl.isLoading) {
        state.refreshCtrl.loadComplete();
      }
      update();
    });
  }

  onRefresh() {
    state.pageIndex = 1;
    queryHistory();
  }

  onLoadding() {
    state.pageIndex += 1;
    queryHistory();
  }
}
